﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace DigitalPlatform.Components
{
    public class ComponentBase : UserControl
    {
        /// <summary>
        /// 删除命令
        /// </summary>
        public ICommand DeleteCommand
        {
            get { return (ICommand)GetValue(DeleteCommandProperty); }
            set { SetValue(DeleteCommandProperty, value); }
        }

        public static readonly DependencyProperty DeleteCommandProperty =
            DependencyProperty.Register("DeleteCommand", typeof(ICommand), typeof(ComponentBase), new PropertyMetadata(null));

        /// <summary>
        /// 删除命令参数
        /// </summary>
        public object DeleteCommandParameter
        {
            get { return (object)GetValue(DeleteCommandParameterProperty); }
            set { SetValue(DeleteCommandParameterProperty, value); }
        }

        public static readonly DependencyProperty DeleteCommandParameterProperty =
            DependencyProperty.Register("DeleteCommandParameter", typeof(object), typeof(ComponentBase), new PropertyMetadata(null));

        /// <summary>
        /// 选中
        /// </summary>
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }
        public static readonly DependencyProperty IsSelectedProperty =
                   DependencyProperty.Register("IsSelected", typeof(bool), typeof(ComponentBase), new PropertyMetadata(false));


        public double ShowWidth
        {
            get { return (double)GetValue(ShowWidthProperty); }
            set { SetValue(ShowWidthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowWidthProperty =
            DependencyProperty.Register("ShowWidth", typeof(double), typeof(ComponentBase), new PropertyMetadata(0d));


        public double ShowHeight
        {
            get { return (double)GetValue(ShowHeightProperty); }
            set { SetValue(ShowHeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowHeightProperty =
            DependencyProperty.Register("ShowHeight", typeof(double), typeof(ComponentBase), new PropertyMetadata(0d));


        /// <summary>
        /// 执行删除命令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Btn_Click(object sender, RoutedEventArgs e)
        {
            DeleteCommand?.Execute(DeleteCommandParameter);
        }
         
        private bool _isMove = false;
        private Point _point = default(Point);

        /// <summary>
        /// 圆点的鼠标按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _isMove = true;
            _point = e.GetPosition((IInputElement)GetParent(this));
            Mouse.Capture((IInputElement)sender);
            e.Handled = true;
        }
        /// <summary>
        /// 圆点鼠标按下并移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Ellipse_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isMove)
            {
                var pos = e.GetPosition((IInputElement)GetParent(this));
                var cursor = (sender as Ellipse).Cursor;
                if (cursor != null)
                {
                    if (cursor == Cursors.SizeNS)
                    {
                        ShowHeight += pos.Y - _point.Y;
                    }
                    else if (cursor == Cursors.SizeWE)
                    {
                        ShowWidth += pos.X - _point.X;
                    }
                    else if (cursor == Cursors.SizeNWSE)
                    {
                        if (Keyboard.Modifiers == ModifierKeys.Control)
                        {
                            ShowHeight += pos.Y - _point.Y;
                            ShowWidth += pos.X - _point.X;
                        }
                        else
                        {
                            // 锁定比例
                            var rate = this.ShowWidth / this.ShowHeight;
                            this.ShowWidth += pos.X - _point.X;
                            this.ShowHeight = this.ShowWidth / rate;
                        }
                    }
                    _point = pos;
                }
                e.Handled = true;
            }
        }
        /// <summary>
        /// 圆点鼠标松开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _isMove = false;
            e.Handled = true;
            Mouse.Capture(null);
        } 
        private Canvas GetParent(DependencyObject child)
        {
            var obj = VisualTreeHelper.GetParent(child);
            if (obj != null && obj is Canvas)
            {
                return obj as Canvas;
            }
            return GetParent(obj);
        }
    }
}
